<?php
/**
 * Created by JetBrains PhpStorm.
 * User: xami
 * Date: 12-8-20
 * Time: 下午8:44
 * To change this template use File | Settings | File Templates.
 *
 *
<!--标题-->
<div class="k-title  k-center-title">
<div class="l">学会活动</div>
<div class="r"><span class="more black"><a href="#">更多</a></span></div>
</div>
<!--列表内容-->
<div class="k-left-content k-center-content">
<ul class="k-content black">
<li><span>2012-07-15</span><a href="#">这里是文章标题不放松的方式</a></li>
<li><span>2012-07-15</span><a href="#">这里是文章标题不放松的方式章标题不放松的章标题不放松的章标题不放松的</a></li>
<li><span>2012-07-15</span><a href="#">这里是文章标题不放松的方式</a></li>
<li><span>2012-07-15</span><a href="#">这里是文章标题不放松的方式</a></li>
<li><span>2012-07-15</span><a href="#">这里是文章标题不放松的方式</a></li>
<li><span>2012-07-15</span><a href="#">这里是文章标题不放松的方式</a></li>
</ul>
</div>
<!--底部圆角-->
<div class="k-fot k-center-fot"> <span class="fotright"></span> </div>
<!--圆角结束-->
 *
 */



class ArticleList extends CWidget
{
    private  $article;
    private $cate;

    private $class_header;
    private $class_body;
    private $class_footer;

    public $show_time=true;
    public $pos;    //left , center, right
    public $cid;
    public $li_class='';

    public function init(){
        $criteria = new CDbCriteria();
        $criteria->condition='`cid` = :cid AND `enabled`=1';
        $criteria->order='`aid` DESC';
        $criteria->params= array(':cid'=>$this->cid);
        $criteria->limit = 6;


        $this->article=Article::model()->findAll($criteria);
        $this->cate=Cate::model()->find('cid=:cid', array(':cid'=>$this->cid));
        if($this->pos=='center'){
            $this->class_header='k-title k-center-title';
            $this->class_body='k-left-content k-center-content';
            $this->class_footer='k-fot k-center-fot';
        }else if($this->pos=='left'){
            $this->class_header='k-title';
            $this->class_body='k-left-content';
            $this->class_footer='k-fot';
        }else if($this->pos=='right'){
            $this->class_header='k-title';
            $this->class_body='k-left-content';
            $this->class_footer='k-fot';
        }
    }

    public function run(){
        $temp='
<!--标题-->
<div class="{{class_header}}">
<div class="l">{{cate_name}}</div>
<div class="r"><span class="more black"><a href="{{more_link}}">更多</a></span></div>
</div>
<!--列表内容-->
<div class="{{class_body}}">
<ul class="k-content {{y}}black">
{{items}}
</ul>
</div>
<!--底部圆角-->
<div class="{{class_footer}}"> <span class="fotright"></span> </div>
<!--圆角结束-->
       ';
         $items='';
//        pd($article->createtime);
        if(!empty($this->article)) foreach($this->article as $article){
             $items.='<li>'.(isset($this->show_time) && $this->show_time ? '<span>'.substr($article->createtime,0,10).'</span>': '').'<a style="font-weight:normal;" href="'.
                 (($this->li_class=='y') ?
                     Yii::app()->createUrl('article/view',array('menu'=>0, 'id'=>$article->aid)) :
                     Yii::app()->createUrl('article/view',array('id'=>$article->aid))).
                 '">'.$article->title.'</a></li>';
        }
        $search=array('{{cate_name}}', '{{more_link}}', '{{class_header}}', '{{class_body}}', '{{class_footer}}', '{{items}}', '{{y}}');
        $replace=array(
            $this->cate->name,
            YII::app()->createUrl('cate/list',array('pid'=>$this->cate->pid, 'cid'=>$this->cate->cid)),
            $this->class_header,
            $this->class_body,
            $this->class_footer,
            $items,
            $this->li_class=='y' ? 'y ':'',
        );
        $html = str_replace($search, $replace, $temp);
        echo $html;
    }
}